import http from './http';
import {LabelValue, LabelValueStatus, LogItem, Page, PageQuery, QueryParams, Result} from '@/types/api';
// @ts-ignore
import qs from "qs";

/**
 * 表信息管理API
 */
export const tableInfoApi = {
    /**
     * 初始化表映射（需要admin角色）
     */
    init: (): Promise<Result<void>> => {
        return http.post('/core/init');
    },

    /**
     * 获取所有表名（过滤忽略表）
     */
    getAllTableName: (): Promise<Result<string[]>> => {
        return http.get('/core/getAllTableName');
    },

    /**
     * 获取表字段
     * @param code 表名
     */
    getAllTableColumn: (code: string): Promise<Result<any>> => {
        return http.get('/core/getAllTableColumn', { params: { code } });
    },

    /**
     * 获取表主键字段
     * @param code 表名
     */
    getPkColumn: (code: string): Promise<Result<string>> => {
        return http.get('/core/getPkColumn', { params: { code } });
    },

    /**
     * 分页查询表数据
     * @param query 分页查询参数
     */
    page: (query: PageQuery): Promise<Result<any>> => {
        return http.post('/core/page', query);
    },

    /**
     * 获取树形结构数据
     * @param code 表名
     */
    tree: (code: string): Promise<Result<any>> => {
        return http.post('/core/tree', null, { params: { code } });
    },

    /**
     * 获取所有数据
     * @param code 表名
     * @param labelParamName label 字段名
     * @param keyParamName value 字段名
     */
    list: (code: string, labelParamName: string,keyParamName:string): Promise<Result<LabelValue[]>> => {
        return http.post('/core/list', null, {
            params: {
                code,
                labelParamName,
                keyParamName
            }
        });
    },

    /**
     * 保存或更新数据
     * @param code 表名
     * @param data 要保存的数据对象
     */
    saveOrUpdate: (code: string, data: any): Promise<Result<any>> => {
        return http.post('/core/saveOrUpdate', null, {
            params: { code, json: JSON.stringify(data) }
        });
    },

    /**
     * 通过主键获取数据
     * @param code 表名
     * @param id 主键值
     */
    getById: (code: string, id: string): Promise<Result<any>> => {
        return http.post('/core/getTableDataById', null, {
            params: { code, id }
        });
    },

    /**
     * 通过主键删除数据
     * @param code 表名
     * @param id 主键值
     */
    deleteById: (code: string, id: string): Promise<Result<boolean>> => {
        return http.post('/core/deleteTableDataById', null, {
            params: { code, id }
        });
    },

    deleteByIds: (code: string, ids: string): Promise<Result<boolean>> => {
        return http.post('/core/deleteTableDataByIds', null, {
            params: { code, ids }
        });
    }
};

export const dicApi = {
    /** 获取字典数据 */
    getDicData: (dicType: string): Promise<Result<LabelValueStatus[]>> => {
        return http.post('/core/getDicDataByDicType', null, {
            params: { dicType }
        });
    },

    /** 刷新字典缓存 */
    refresh: (dicType: string): Promise<Result<void>> => {
        return http.get(`/core/refreshDic/${dicType}`);
    },
};

export const logApi = {
    page: (params: QueryParams): Promise<Result<Page<LogItem>>> => {
        return http.post('/log/page', qs.stringify(params))
    },
}

export const chatApi = {
    sendMessage: async (content: string) => {
       return http.post('/core/chat', null,{
           params: { content }
       });
    },
    aiFill: async (content: string,data: any,dicts:string) => {
        return http.post('/core/aiFill', {
            content,
            form: JSON.stringify(data),
            dicts
        });
    },
}